package com.txm.qqserver.service;

import com.txm.qqcommon.File_;
import com.txm.qqcommon.Message;
import com.txm.qqcommon.MessageType;
import com.txm.qqcommon.Response;
import com.txm.qqdatabase.DB;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class HandleTransferFile extends Thread {
    Response res;
    ServerConnectServerThread scst;

    static String filePath;


    static {
        URI uri = null;
        try {
            uri = HandleTransferFile.class.getResource("/").toURI();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
        Path path = Paths.get(uri).getParent().getParent().getParent();
        filePath = path.toString();
        System.out.println("filePath:" + filePath);
    }


    public HandleTransferFile() {
    }

    public HandleTransferFile(Response res) {
        this.res = res;
    }

    public HandleTransferFile(Response res, ServerConnectServerThread scst) {
        this.res = res;
        this.scst = scst;
    }

    @Override
    public void run() {
        System.out.println("开始接收文件");
        Message msg = (Message) res.getObject();

        File_ file_info = (File_) msg.getObject();


        this.setName(msg.getSender() + Message.getLocalDate());

        ServerConnectServerThread thread = (ServerConnectServerThread) ManageClientThreads.getMcts().get(msg.getSender());
        try {

            File file = new File(filePath + "\\transferFile");

            if (!file.exists()) {
                if (!file.mkdir()) System.out.println("文件夹创建失败。");
            } else {
                System.out.println("文件夹已存在。");
            }
            File file1 = new File(file, file_info.getFileName());
            if (!file1.createNewFile()) System.out.println("文件已存在。");

            FileOutputStream fos = new FileOutputStream(file1);
            fos.write(file_info.getDataCut(), 0, Long.valueOf(file_info.getDataCut_size()).intValue());
            file_info.getProgress();
            while (true) {
                ObjectInputStream ois = new ObjectInputStream(thread.accept.getInputStream());
                Response res = (Response) ois.readObject();

                Message msg_ = (Message) res.getObject();
                File_ file_info_ = (File_) msg_.getObject();

//                System.out.println(this.getName() + res);
                fos.write(file_info_.getDataCut(), 0, Long.valueOf(file_info_.getDataCut_size()).intValue());
                file_info_.getProgress();

                if (file_info_.getStatus() == -1) {
                    break;
                }
            }
            fos.close();
            System.out.println("文件接收完成");
            new SendFile(res, file1).start();
        } catch (Exception e) {
            System.out.println(this.getName() + ":" + e);
//            e.printStackTrace();
        }
    }


    public static void handleFileMsg(Response res, ServerConnectServerThread scst) {
        String path = res.getPath();
        if (!path.equals("/file")) return;
        Message msg = (Message) res.getObject();
        if (msg.getMsgType().equals(MessageType.MESSAGE_SENDER_FILE_USER)) {
            Message message = new Message();
            message.setContent("验证成功");
            message.setGetter(msg.getSender());
            message.setCurrentLocalDate();
            message.setMsgType(MessageType.MESSAGE_SENDER_FILE_USER);

            if (!DB.checkUser(msg.getGetter())) {
                message.setMsgType(MessageType.MESSAGE);
                message.setContent(msg.getGetter() + "用户不存在。");
            }

            ServerConnectServerThread thread = (ServerConnectServerThread) ManageClientThreads.getMcts().get(msg.getSender());
//            System.out.println("back" + message);
            try {
                ObjectOutputStream objectOutputStream = new ObjectOutputStream(thread.accept.getOutputStream());
                objectOutputStream.writeObject(message);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
